package io.sdl.common.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.sdl.common.service.impl.CrudServiceImpl;
import io.sdl.common.sys.dao.SdlDoctorsDao;
import io.sdl.common.sys.dto.SdlDoctorsDTO;
import io.sdl.common.sys.entity.SdlDoctorsEntity;
import io.sdl.common.sys.service.SdlDoctorsService;
import cn.hutool.core.util.StrUtil;
import io.sdl.common.sys.vo.SdlDoctorsVo;
import io.sdl.common.utils.UploadFileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Dc 1211734836@qq.com
 * @since 1.0.0 2025-03-28
 */
@Service
public class SdlDoctorsServiceImpl extends CrudServiceImpl<SdlDoctorsDao, SdlDoctorsEntity, SdlDoctorsDTO> implements SdlDoctorsService {

	@Override
	public QueryWrapper<SdlDoctorsEntity> getWrapper(Map<String, Object> params) {
		String id = (String) params.get("id");

		QueryWrapper<SdlDoctorsEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StrUtil.isNotBlank(id), "id", id);

		return wrapper;
	}


	@Override
	public SdlDoctorsEntity findByPhone(String phone) {


		return baseDao.findByPhone(phone);
	}

	@Override
	public Integer saveUploadedFile(MultipartFile file, Long id) {
		try {

			String accessUrl = "/uploads/" + UploadFileUtils.uploadFile(file);

			SdlDoctorsEntity sdlDoctorsEntity = baseDao.selectById(id);

			sdlDoctorsEntity.setCertificates(sdlDoctorsEntity.getCertificates() != null ?
					(sdlDoctorsEntity.getCertificates() + "," + accessUrl) : accessUrl);
			return baseDao.updateById(sdlDoctorsEntity);
		} catch (IOException e) {
			throw new RuntimeException("文件存储失败", e);
		}
	}

	@Override
	public List<SdlDoctorsVo> findAll(Map<String, Object> params) {
		return baseDao.findAll(params);
	}

	@Override
	public SdlDoctorsVo getDetail(Long id) {
		return baseDao.getDetail(id);
	}
}